//当前页数
var page = 1;
//每页显示的行数
var limit = 10;
//总行数
var totalCount = 0;
//总页数
var totalPage = 0;

$(function(){
	//查询所有商品类型
	var url = "../productType/findAllNameOrId.action";
	var data = null;
	$.ajax({
		url:url,
		data:data,
		success:function(msg){
			$("#productType").html("");
			for(var i=0;i<msg.length;i++){
				$("#productType").append('<li id='+msg[i].proTypeId+'><a href="javascript:findType(\''+msg[i].proTypeId+'\');">'+msg[i].proTypeName+'</a></li>');
			}
		}
	});
	//查询所有商品
	var data = {"page":page,"limit":limit,"isva":"2"};
	findAllProduct(data);
	
	//search
	$("#search").click(function(){
		var value = $("#search-kw").val();
		var data = {"page":page,"limit":limit,"proName":value};
		findAllProduct(data);
	});
	 
})

//查询所有商品的方法
function findAllProduct(data){
	var url = "../product/findAll.action";
	$.ajax({
		url:url,
		data:data,
		async:false,
		success:function(msg){
			$("#product").html("");
			totalCount = msg.count;
			var productList = msg.data;
			for(var i=0;i<productList.length;i++){
				$("#product").append('<li><div class="pro-panels"><p class="p-img"><a target="_blank" href="productDetail.html?proId='+productList[i].proId+'"'
								+'title="'+productList[i].proName+'"><img alt="'+productList[i].proName+'"'
								+'src="../'+productList[i].imgPath+'"></a></p><p class="p-name"><a target="_blank"'
								+'href="productDetail.html?proId='+productList[i].proId+'" title="'+productList[i].proName+'"> <span>'+productList[i].proName+'</span> <span class="red"></span>'
								+'</a></p><p class="p-price"><b>¥'+productList[i].proPrice+'</b></p><div class="p-button clearfix"><table colspan="0" border="0" rowspan="0">'
								+'<tbody><tr><td><a target="_blank" href="javascript:;" class="p-button-cart"><span>立即购买</span></a></td>'
								+'<td><label class="p-button-score"><span>52167人评价</span></label></td></tr></tbody></table></div></div></li>');
			} 
			//得到总页数
			if(totalCount % limit == 0){
				totalPage = totalCount / limit;
			}else {
				totalPage = (totalCount / limit) + 1;
			}
			$("#page_ul").html("");
			$("#page_ul").append('<li class="pgNext link" onclick="findPages(\'reduce\');">&lt;</li>');
			for(var i=0;i<parseInt(totalPage);i++){
				/*if( i == 0){
					$("#page_ul").append('<a id="page_'+(i+1)+'" href="javascript:findPage(\''+(i+1)+'\');"'
							+'title="第'+(i+1)+'页"><li class="page-number link pgCurrent">'+(i+1)+'</li></a>');
				}else {*/
					$("#page_ul").append('<a id="page_'+(i+1)+'" href="javascript:findPage(\''+(i+1)+'\');"'
							+'title="第'+(i+1)+'页"><li class="page-number link" id=num_'+(i+1)+'>'+(i+1)+'</li></a>');
				/*}*/
			}
			$("#page_ul").append('<li class="pgNext link" onclick="findPages(\'plus\');">&gt;</li>');
			$("#num_"+data.page).attr("class","page-number link pgCurrent");
		}
	});
}


//分页查询
function findPage(num){
	page = num;
	var data = {"page":page,"limit":limit};
	findAllProduct(data);
}
function findPages(str){
	var id = $(".pgCurrent");
	var strs = $(id).parent().attr("id");
	var arr = strs.split("_");
	if(str == "reduce"){
		if((parseInt(arr[1]) - parseInt(1)) == 0){
			alert("第一页");
			page = 1;
			return ;
		}else {
			page = parseInt(arr[1]) - parseInt(1);
		}
	}else {
		if((parseInt(arr[1])+parseInt(1)) > totalPage){
			alert("最后一页");
			page = totalPage;
			return ;
		}else {
			page = parseInt(arr[1])+parseInt(1);
		}
	}

	$("#page_"+page).attr("class","page-number link pgCurrent");
	var data = {"page":page,"limit":limit};
	findAllProduct(data);
}

//分类查询
function findType(num){
	var data = "";
	if(num == "all"){
		data = {"page":page,"limit":limit,"isva":"2"};
	}else {
		data = {"page":page,"limit":limit,"proTypeId":num,"isva":"2"};
	}
	findAllProduct(data);
	var obj = $(".selected");
	$(obj).removeAttr("class");
	$("#"+num).attr("class","selected");
}

//退出登录
function exit(str){
	var url = "../user/exit.action";
	var data = {"action":str};
	$.ajax({
		url:url,
		data:data,
		success:function(msg){
			parent.location.reload();
		}
	});
}

layui.use(['laydate', 'laypage', 'layer', 'table', 'upload', 'element'], function(){
	  var laydate = layui.laydate //日期
	  ,laypage = layui.laypage //分页
	  layer = layui.layer //弹层
	  ,table = layui.table //表格
	  ,upload = layui.upload //上传
	  ,element = layui.element; //元素操作
	 
	
	  var $ = layui.$;
	  $("#top-index-loginUrl").click(function(){
		  layer.open({
			  title: ' '
			  ,type: 2
			  ,area: ['38%','74%']
			  ,content: 'login.html'
			});
	  });
	  $("#top-index-registUrl").click(function(){
		  layer.open({
			  title: ' '
			  ,type: 2
			  ,area: ['38%','74%']
			  ,content: 'regist.html'
			});
	  });
	  
	
	  
	  if(user != null && user != ""){
			//申请成为商家的审核提示
			  if(user.isva == "2"){
				  layer.open({
					  title: '提示',
					  content: '您提交的成为商家的审核,管理员审核通过,恭喜您成为商家,现在您可以去个人中心添加您的商品',
					  btn: ['确定'],
					  yes: function(index,layero){
						  var url = "../user/saveOrUpdate.action";
						  var data = {"usersId":user.usersId,"isva":"3"};
						  $.ajax({
							  url:url,
							  data:data,
							  success:function(msg){
								  parent.location.reload();
							  }
						  });
					  }
				  });
			  }else if(user.isva == "0"){
				  layer.open({
					  title: '提示',
					  content: '您提交的成为商家的审核,管理员审核不通过',
					  btn: ['确定'],
					  yes: function(index,layero){
						  var url = "../user/saveOrUpdate.action";
						  var data = {"usersId":user.usersId,"isva":"3"};
						  $.ajax({
							  url:url,
							  data:data,
							  success:function(msg){
								  parent.location.reload();
							  }
						  });
					  }
				  });
			  }
		}
	});